package com.wanmait.schedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wanmait.schedule.entity.Apply;
import com.wanmait.schedule.entity.CompanyUser;
import com.wanmait.schedule.entity.Department;
import com.wanmait.schedule.mapper.*;
import com.wanmait.schedule.service.ApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 申请表	 服务实现类
 * </p>
 *
 * @author wanmait
 * @since 2022-03-16
 */
@Service
public class ApplyServiceImpl extends ServiceImpl<ApplyMapper, Apply> implements ApplyService {

    @Resource
    private ApplyMapper applyMapper;
    @Resource
    private UserinfoMapper userinfoMapper;
    @Resource
    private DepartmentMapper departmentMapper;
    @Resource
    private CompanyUserMapper companyUserMapper;
    @Resource
    private CompanyMapper companyMapper;

    @Resource
    private PositionMapper positionMapper;

    @Override
    public List<Apply> initApply(Integer companyId) {
        LambdaQueryWrapper<Apply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Apply::getCompanyId,companyId);
        List<Apply> applyList = applyMapper.selectList(queryWrapper);
        applyList.forEach(apply -> {
            apply.setUserinfo(userinfoMapper.selectById(apply.getUserinfoId()));
        });
        return applyList;
    }

    @Override
    public void agree(Integer id) {
        applyMapper.agree(id);
    }

    @Override
    public Boolean refuse(Integer id) {
        if (applyMapper.refuse(id)==true){
            return true;
        }
        else {
            return false;
        }
    }

    @Override
    public List<Department> initDepartment(Integer companyId) {
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Department::getCompanyId,companyId);
        return departmentMapper.selectList(queryWrapper);
    }

    @Override
    public Boolean distribution(CompanyUser companyUser) {
        companyUserMapper.insert(companyUser);
        return true;
    }

    @Override
    public List<CompanyUser> initCompanyUser(Integer companyId) {
        LambdaQueryWrapper<CompanyUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CompanyUser::getCompanyId,companyId);
        queryWrapper.ne(CompanyUser::getPositionId,1);
        List<CompanyUser> companyUsers = companyUserMapper.selectList(queryWrapper);
        companyUsers.forEach(companyUser -> {
            companyUser.setUserinfo(userinfoMapper.selectById(companyUser.getUserinfoId()));
            companyUser.setPosition(positionMapper.selectById(companyUser.getPositionId()));
        });
        return companyUsers;
    }

    @Override
    public List<Department> selectDepartment(Integer id,Integer companyId) {
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        CompanyUser companyUser = companyUserMapper.selectById(id);
        if (companyUser.getDepartmentId() != null){
            queryWrapper.eq(Department::getCompanyId,companyId);
            queryWrapper.ne(Department::getId,companyUser.getDepartmentId());
            List<Department> departments = departmentMapper.selectList(queryWrapper);
            return departments;
        }
        else {
            queryWrapper.eq(Department::getCompanyId,companyId);
            List<Department> departments = departmentMapper.selectList(queryWrapper);
            return departments;
        }

    }

    @Override
    public Boolean assignment(Integer id, Integer departmentId) {
        LambdaUpdateWrapper<CompanyUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(CompanyUser::getId,id);
        CompanyUser companyUser = new CompanyUser();
        companyUser.setDepartmentId(departmentId);
        int flag = companyUserMapper.update(companyUser,updateWrapper);
        if (flag==1){
            return true;
        }else {
            return false;
        }

    }


    @Override
    public List<Apply> findAll() {

        List<Apply> applies = this.list();
        applies.forEach(apply -> {
            apply.setCompany(companyMapper.selectById(apply.getCompanyId()));
            System.out.println(apply.getUserinfoId());
            apply.setUserinfo(userinfoMapper.selectById(apply.getUserinfoId()));
        });
        return applies;
    }

    @Override
    public Apply isApplied(Integer companyId, Integer userinfoId) {
        LambdaQueryWrapper<Apply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Apply::getCompanyId, companyId);
        queryWrapper.eq(Apply::getUserinfoId,userinfoId);
        Apply existApply = applyMapper.selectOne(queryWrapper);
        if(existApply != null){
            return existApply;
        }
        return null;
    }
}
